package com.example.springbatchdemo.future;

import java.util.concurrent.*;

/**
 * @Classname FutureInAction2
 * @Description TODO
 * @Date 2020/9/26 19:07
 * @Created by kevin
 */
public class FutureInAction2 {
    public static void main( String[] args ) throws ExecutionException, InterruptedException {

        // 创建单线程任务
        ExecutorService executorService = Executors.newSingleThreadExecutor();

        Future<String> future = executorService.submit(() -> {
            try {
                TimeUnit.SECONDS.sleep(10);
                return "i am finished";
            } catch (InterruptedException e) {
                return "i am error";
            }
        });

        while (!future.isDone()){
            TimeUnit.MICROSECONDS.sleep(10);
        }
        System.out.println(future.get()+"\t"+future.isDone()+"\t"+future.isCancelled());

        // 关闭执行任务
        executorService.shutdown();
    }
}
